package com.buka.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

@org.springframework.stereotype.Service
public class Service {

    @Resource
    RabbitTemplate rabbitTemplate;
    @Resource
    RedisTemplate redisTemplate;

    String locaion_key = ":location";
    String status_key = ":status";
    String distance_key = ":distance";

    public void start(String uid) {
        //开启行程记录功能
        redisTemplate.opsForValue().set(uid + status_key, true);
        //经度
        double longitude = 116.397155;
        //原子类
        AtomicReference<Double> _longitude = new AtomicReference(longitude);
        //纬度
        double latitude = 39.916344;
        //初始位置对象
        Point point = new Point(longitude, latitude);
        //将其实位置存入redis
        redisTemplate.opsForGeo().add(uid + locaion_key, point, "start");
        //创建线程方式

        new Thread(new Runnable() {
            @Override
            public void run() {
                //循环条件应该是变量  变量从哪来？
                while ((boolean) redisTemplate.opsForValue().get(uid + status_key)) {

                    //坐标累加，模拟移动
                    Double aLong = _longitude.get();//每秒移动2.57KM
                    _longitude.set(aLong + 0.025D);

                    System.out.println("位置信息：" + _longitude + ":" + latitude);

                    Map<String, Double> hashMap = new HashMap<>();
                    hashMap.put("latitude", latitude);
                    hashMap.put("longitude", _longitude.get());

                    rabbitTemplate.convertAndSend("buka", "location", hashMap);
                    System.out.println("发送位置信息" + hashMap);
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                //停止发送位置信息，记录结束地址
                Point point = new Point(_longitude.get(), latitude);
                redisTemplate.opsForGeo().add(uid + locaion_key, point, "end");

                //计算距离
                Distance distance = redisTemplate.opsForGeo().distance(uid + locaion_key, "start", "end", Metrics.KILOMETERS);
                String s = distance.toString();//2KM

                redisTemplate.opsForValue().set(uid + distance_key, s);
            }
        }).start();

    }

    /**
     * 停止发送位置信息
     * 每个用户的自己行程
     *
     * @return
     */
    public String stop(String uid) throws InterruptedException {
        redisTemplate.opsForValue().set(uid + status_key, false);
        while (!redisTemplate.hasKey(uid + distance_key)) {
            TimeUnit.MILLISECONDS.sleep(300);
        }
        String s = redisTemplate.opsForValue().get(uid + distance_key).toString();
        //清楚redis中数据
        redisTemplate.delete(uid + status_key);
        redisTemplate.delete(uid + locaion_key);
        redisTemplate.delete(uid + distance_key);

        return s;
    }

}
